<template>
  <Dialog v-model="dialogVisible" title="坐席详情" width="90%" class="owner-stat-dialog">
    <!-- 搜索工作栏 -->
    <div class="search-container">
      <el-form
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="100px"
        label-position="left"
        class="search-form"
      >
        <div class="search-row">
          <el-form-item label="归属部门" prop="deptId" class="search-item">
            <el-tree-select
              v-model="queryParams.deptId"
              :data="deptList"
              :props="defaultProps"
              check-strictly
              node-key="id"
              placeholder="请选择归属部门"
              class="search-tree-select"
            >
              <template #prefix>
                <Icon icon="ep:office-building" class="input-icon" />
              </template>
            </el-tree-select>
          </el-form-item>

          <el-form-item label="坐席号码" prop="agentNumber" class="search-item">
            <el-input
              v-model="queryParams.agentNumber"
              placeholder="请输入坐席号码"
              clearable
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:user" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>
        </div>

        <div class="search-actions">
          <el-button
            type="success"
            class="export-btn"
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['call:stat:export']"
          >
            <Icon icon="ep:download" class="btn-icon" />
            <span class="btn-text">导出</span>
          </el-button>
          <el-button class="search-btn" @click="handleQuery">
            <Icon icon="ep:search" class="btn-icon" />
            <span class="btn-text">搜索</span>
          </el-button>
          <el-button class="reset-btn" @click="resetQuery">
            <Icon icon="ep:refresh" class="btn-icon" />
            <span class="btn-text">重置</span>
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 表格容器 -->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        @sort-change="sortChange"
        :show-overflow-tooltip="true"
        class="owner-table"
      >
        <el-table-column
          label="时间"
          align="center"
          prop="datetime"
          :sortable="true"
          min-width="120"
        >
          <template #default="scope">
            <div class="time-cell">
              <Icon icon="ep:calendar" class="time-icon" />
              <span class="time-text">{{ scope.row.datetime }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="坐席工号" align="center" prop="agentNumber" min-width="120">
          <template #default="scope">
            <div class="agent-cell">
              <Icon icon="ep:user" class="agent-icon" />
              <span class="agent-text">{{ scope.row.agentNumber }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="员工" align="center" prop="nickname" min-width="100">
          <template #default="scope">
            <div class="employee-cell">
              <Icon icon="ep:avatar" class="employee-icon" />
              <span class="employee-text">{{ scope.row.nickname }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="部门" align="center" prop="deptName" min-width="120">
          <template #default="scope">
            <div class="dept-cell">
              <Icon icon="ep:office-building" class="dept-icon" />
              <span class="dept-text">{{ scope.row.deptName }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="外呼总量"
          align="center"
          prop="totalCount"
          :sortable="true"
          min-width="120"
        >
          <template #default="scope">
            <div class="count-cell">
              <Icon icon="ep:phone" class="count-icon" />
              <span class="count-text">{{ scope.row.totalCount }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="接通量"
          align="center"
          prop="successCount"
          :sortable="true"
          min-width="120"
        >
          <template #default="scope">
            <div class="success-cell">
              <Icon icon="ep:check" class="success-icon" />
              <span class="success-text">{{ scope.row.successCount }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="接通率" align="center" prop="rate" :sortable="true" min-width="120">
          <template #default="scope">
            <div class="rate-cell">
              <el-progress
                :percentage="parseFloat(scope.row.rate)"
                :color="getRateColor(scope.row.rate)"
                :stroke-width="8"
                class="rate-progress"
              />
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="通话时长"
          align="center"
          prop="callSecond"
          :sortable="true"
          min-width="140"
        >
          <template #default="scope">
            <div class="duration-cell">
              <Icon icon="ep:timer" class="duration-icon" />
              <span class="duration-text">{{ convertSecondsToHMS(scope.row.callSecond) }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="计费分钟"
          align="center"
          prop="callMinute"
          :sortable="true"
          min-width="120"
        >
          <template #default="scope">
            <div class="minute-cell">
              <Icon icon="ep:clock" class="minute-icon" />
              <span class="minute-text">{{ scope.row.callMinute }}分钟</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="使用话费"
          align="center"
          prop="callCostPrice"
          :sortable="true"
          min-width="120"
        >
          <template #default="scope">
            <div class="cost-cell">
              <Icon icon="ep:money" class="cost-icon" />
              <span class="cost-text">¥{{ scope.row.callSalePrice.toFixed(2) }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </Dialog>
</template>

<script setup>
import { StatisticsRankApi } from '@/api/call/statistics/index'
import { convertSecondsToHMS } from '@/utils/formatTime'
import download from '@/utils/download'
import { defaultProps, handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'
import * as proxyApi from '@/api/home/proxy.ts'

const dialogVisible = ref(false)
const queryParams = reactive({
  deptId: null,
  times: [],
  group: 'day',
  sortStr: '',
  sortFields: {}
})

const loading = ref(false)
const list = ref([])
const deptList = ref([])

// 获取接通率颜色
const getRateColor = (rate) => {
  const rateNum = parseFloat(rate)
  if (rateNum >= 80) return '#67c23a'
  if (rateNum >= 60) return '#e6a23c'
  if (rateNum >= 40) return '#f56c6c'
  return '#909399'
}

// 列表
const getList = async () => {
  try {
    if (Object.keys(queryParams.sortFields).length > 0) {
      queryParams.sortStr = JSON.stringify(queryParams.sortFields)
    }
    let data
    if (tenantId.value) {
      queryParams.tenantId = tenantId.value
      data = await proxyApi.ownerProxy(queryParams)
    } else {
      data = await StatisticsRankApi.getStatOwner(queryParams)
    }
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

const queryFormRef = ref(null)
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryFormRef.sortFields = {}
  handleQuery()
}
/*导出*/
const exportLoading = ref(false)
const handleExport = async () => {
  try {
    // 发起导出
    exportLoading.value = true
    let data
    if (tenantId.value) {
      queryParams.tenantId = tenantId.value
      data = await proxyApi.OwnerexportExcelByProxy(queryParams)
    } else {
      data = await StatisticsRankApi.exportStatOwner(queryParams)
    }
    download.excel(data, '坐席数据报表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const sortChange = (column) => {
  queryParams.sortFields[column.prop] = column.order == 'descending' ? 'desc' : 'asc'
  handleQuery()
}
const tenantId = ref(false)
/*显示*/
const open = async (params, times, val) => {
  dialogVisible.value = true
  queryParams.deptId = params.deptId
  queryParams.group = params.group
  queryParams.times = times
  tenantId.value = val
  deptList.value = handleTree(await DeptApi.getSimpleDeptList())
  getList()
}

defineExpose({ open })
</script>

<style lang="scss" scoped>
// 对话框样式
.owner-stat-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    overflow: hidden;

    .el-dialog__header {
      background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
      color: white;
      padding: 20px 24px;

      .el-dialog__title {
        color: white;
        font-size: 18px;
        font-weight: 600;
      }

      .el-dialog__headerbtn {
        .el-dialog__close {
          color: rgba(255, 255, 255, 0.8);
          font-size: 18px;

          &:hover {
            color: white;
          }
        }
      }
    }

    .el-dialog__body {
      padding: 0;
    }
  }
}

// 搜索容器
.search-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.search-form {
  .search-row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
  }

  .search-item {
    flex: 1;
    min-width: 300px;
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
    }
  }
}

.search-tree-select {
  width: 100%;

  :deep(.el-tree-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focused {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.search-btn,
.reset-btn,
.export-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    font-size: 16px;
  }

  &:hover {
    transform: translateY(-1px);
  }
}

.search-btn {
  background: #f3f4f6;
  border: 2px solid #e5e7eb;
  color: #374151;

  &:hover {
    background: #e5e7eb;
    border-color: #d1d5db;
  }
}

.reset-btn {
  background: #fef3c7;
  border: 2px solid #fbbf24;
  color: #92400e;

  &:hover {
    background: #fde68a;
    border-color: #f59e0b;
  }
}

.export-btn {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border: none;
  color: white;

  &:hover {
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
  }
}

// 表格容器
.table-container {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.owner-table {
  :deep(.el-table__header) {
    background: #f8fafc;

    th {
      background: #f8fafc !important;
      color: #374151;
      font-weight: 600;
      border-bottom: 2px solid #e5e7eb;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.2s ease;

      &:hover {
        background: #f8fafc !important;
      }

      td {
        padding: 16px 12px;
        border-bottom: 1px solid #f1f5f9;
      }
    }
  }
}

// 表格单元格样式
.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .time-icon {
    color: #667eea;
    font-size: 14px;
  }

  .time-text {
    color: #374151;
    font-weight: 500;
  }
}

.agent-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .agent-icon {
    color: #3b82f6;
    font-size: 14px;
  }

  .agent-text {
    color: #374151;
    font-weight: 600;
    font-family: 'Courier New', monospace;
  }
}

.employee-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .employee-icon {
    color: #8b5cf6;
    font-size: 14px;
  }

  .employee-text {
    color: #374151;
    font-weight: 500;
  }
}

.dept-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .dept-icon {
    color: #f59e0b;
    font-size: 14px;
  }

  .dept-text {
    color: #374151;
    font-weight: 500;
  }
}

.count-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .count-icon {
    color: #3b82f6;
    font-size: 14px;
  }

  .count-text {
    color: #374151;
    font-weight: 600;
  }
}

.success-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .success-icon {
    color: #10b981;
    font-size: 14px;
  }

  .success-text {
    color: #374151;
    font-weight: 600;
  }
}

.rate-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;

  .rate-progress {
    width: 80px;

    :deep(.el-progress-bar__outer) {
      border-radius: 4px;
    }

    :deep(.el-progress-bar__inner) {
      border-radius: 4px;
    }
  }

  .rate-text {
    font-size: 12px;
    color: #6b7280;
    font-weight: 500;
  }
}

.duration-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .duration-icon {
    color: #f59e0b;
    font-size: 14px;
  }

  .duration-text {
    color: #374151;
    font-weight: 500;
    font-family: 'Courier New', monospace;
  }
}

.minute-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .minute-icon {
    color: #8b5cf6;
    font-size: 14px;
  }

  .minute-text {
    color: #374151;
    font-weight: 500;
  }
}

.cost-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .cost-icon {
    color: #ef4444;
    font-size: 14px;
  }

  .cost-text {
    color: #ef4444;
    font-weight: 600;
    font-family: 'Courier New', monospace;
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .owner-stat-dialog {
    :deep(.el-dialog) {
      width: 95% !important;
      margin: 20px auto;

      .el-dialog__header {
        padding: 16px 20px;

        .el-dialog__title {
          font-size: 16px;
        }
      }
    }
  }

  .search-container {
    padding: 16px;

    .search-row {
      flex-direction: column;
      gap: 16px;

      .search-item {
        min-width: auto;
      }
    }

    .search-actions {
      justify-content: stretch;

      .search-btn,
      .reset-btn,
      .export-btn {
        flex: 1;
        justify-content: center;
      }
    }
  }

  .table-container {
    overflow-x: auto;

    .owner-table {
      min-width: 1200px;
    }
  }

  .rate-cell {
    .rate-progress {
      width: 60px;
    }
  }
}

@media (max-width: 480px) {
  .owner-stat-dialog {
    :deep(.el-dialog) {
      width: 100% !important;
      margin: 0;
      height: 100vh;
      border-radius: 0;
    }
  }

  .search-container {
    padding: 12px;

    .search-actions {
      flex-direction: column;
      gap: 8px;
    }
  }

  .owner-table {
    :deep(.el-table__header) {
      th {
        padding: 12px 8px;
        font-size: 13px;
      }
    }

    :deep(.el-table__body) {
      tr td {
        padding: 12px 8px;
      }
    }
  }

  .btn-text {
    display: none;
  }

  .rate-cell {
    .rate-progress {
      width: 50px;
    }

    .rate-text {
      font-size: 11px;
    }
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .search-container,
  .table-container {
    background: #1e293b;
    border-color: #334155;
  }

  .owner-table {
    :deep(.el-table__header) {
      th {
        background: #0f172a !important;
        color: #f1f5f9;
        border-color: #334155;
      }
    }

    :deep(.el-table__body) {
      tr {
        &:hover {
          background: #334155 !important;
        }

        td {
          border-color: #334155;
        }
      }
    }
  }

  .time-cell .time-text,
  .agent-cell .agent-text,
  .employee-cell .employee-text,
  .dept-cell .dept-text,
  .count-cell .count-text,
  .success-cell .success-text,
  .duration-cell .duration-text,
  .minute-cell .minute-text {
    color: #f1f5f9;
  }
}
</style>
